package com.hsnn.medstgmini.trading.pur.controller;

import java.util.Enumeration;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.hsnn.medstgmini.trading.pur.model.OrderPackStatus;
import com.hsnn.medstgmini.Constants;
import com.hsnn.medstgmini.base.dict.model.form.DicAreaForm;
import com.hsnn.medstgmini.base.dict.service.DicAreaManager;
import com.hsnn.medstgmini.base.std.model.HospInfo;
import com.hsnn.medstgmini.base.std.model.StdCompinfo;
import com.hsnn.medstgmini.sys.model.SysUser;
import com.hsnn.medstgmini.trading.pur.model.form.OrderPackage;
import com.hsnn.medstgmini.trading.pur.service.PurOrderInfoOldManager;
import com.hsnn.medstgmini.util.DbUtil;
import com.hsnn.medstgmini.util.Pagination;

/**
 * @category 中心端查看历史订单明细
 * @author 应晓川
 * @date 2015年7月9日
 */
@Controller
@RequestMapping("/purOrderInfoOld")
public class PurOrderInfoOldController {
	
private static final Logger log = Logger.getLogger(PurOrderInfoOldController.class);
	
	@Autowired
	private PurOrderInfoOldManager purOrderInfoOldManager;
	
	@Autowired
	private DicAreaManager dicAreaManager;
	
	@Autowired
	private HttpServletRequest request;

	@Autowired
	private HttpSession session;
	
	@Value("#{sysProp['system.province']}")
	private String sysProvince;
	
	/**
	 * @category 进入历史订单明细页面[中心端]
	 * 应晓川
	 * @date 2015年7月9日
	 * @return
	 */
	@RequestMapping("/toOrderInfoOld")
	public String toOrderInfoOld() {
		DicAreaForm area = dicAreaManager.getDicAreaById(sysProvince);
		request.setAttribute("area", area);
		request.setAttribute("sysAreaId", sysProvince);
		return "/trading/pur/purOrderInfoOld";
	}
	
	/**
	 * @category 获取历史订单数据[中心端]
	 * 应晓川
	 * @date 2015年7月9日
	 * @return
	 */
	@RequestMapping("/getOrdersDetailListOld")
	@ResponseBody
	public Pagination getOrdersDetailListOld(OrderPackage orderPackage) {
		log.info("enter getOrdersDetailListOld method");
		Pagination page = new Pagination(request);
		try {
			page.setForm(orderPackage);
			page = purOrderInfoOldManager.getOrdersDetailListOld(page);
			page.setSuccess(true);
		} catch (Exception e) {
			log.error("",e);
		}
		return page;
		
	}
	/**
	 * @category 跳转至历史订单明细页面[生产企业端]
	 * 应晓川
	 * @date 2015年7月9日
	 * @return
	 */
	@RequestMapping("/toOrderInfoOldByProComp")
	public String toOrderInfoOldByProComp() {
		DicAreaForm area = dicAreaManager.getDicAreaById(sysProvince);
		request.setAttribute("area", area);
		request.setAttribute("sysAreaId", sysProvince);
		return "/trading/pur/purOrderInfoOldByProComp";
	}
	
	/**
	 * @category 获取历史订单数据[生产企业端]
	 * 应晓川
	 * @date 2015年7月9日
	 * @return
	 */
	@RequestMapping("/getOrdersDetailListOldByProComp")
	@ResponseBody
	public Pagination getOrdersDetailListOldByProComp() {
		log.info("enter getOrdersDetailListOldByProComp method");
		Pagination page = new Pagination(request);
		SysUser user = (SysUser) request.getSession().getAttribute(Constants.USERINFO);
		String prodCompcode = ((StdCompinfo )user.getInfo()).getCompCode();
		try {
			DbUtil.jsonToMap(request, page);
			page.getConditions().put("prodCompcode", prodCompcode);
			page = purOrderInfoOldManager.getOrdersDetailListOldByProComp(page);
			page.setSuccess(true);
		} catch (Exception e) {
			log.error("",e);
			page.setSuccess(false);
		}
		return page;
	}
	/////////////////////////
	
	/**
	 * @category 跳转至历史订单明细页面[生产企业端]
	 * 应晓川
	 * @date 2015年7月9日
	 * @return
	 */
	@RequestMapping("/toOrderInfoOldByComp")
	public String toOrderInfoOldByComp() {
		DicAreaForm area = dicAreaManager.getDicAreaById(sysProvince);
		request.setAttribute("area", area);
		request.setAttribute("sysAreaId", sysProvince);
		return "/trading/pur/purOrderInfoOldByComp";
	}
	
	/**
	 * @category 获取历史订单数据[生产企业端]
	 * 应晓川
	 * @date 2015年7月9日
	 * @return
	 */
	@RequestMapping("/getOrdersDetailListOldByComp")
	@ResponseBody
	public Pagination getOrdersDetailListOldByComp(OrderPackage orderPackage) {
		log.info("enter getOrdersDetailListOldByComp method");
		Pagination page = new Pagination(request);
		SysUser user = (SysUser) request.getSession().getAttribute(Constants.USERINFO);
		try {
			orderPackage.setOrderStatus(OrderPackStatus.submit.getKey());
			//	获取配送企业code
			StdCompinfo comp = user.getInfo();
			orderPackage.setDelCompCode(comp.getCompCode());
			page.setForm(orderPackage);
			page = purOrderInfoOldManager.getOrdersDetailListOldByComp(page);
			page.setSuccess(true);
		} catch (Exception e) {
			log.error("",e);
		}
		return page;
	}
	
	
	
	/**
	 * @category 跳转至历史订单明细页面[医疗机构端]
	 * 应晓川
	 * @date 2015年7月9日
	 * @return
	 */
	@RequestMapping("/toOrderInfoOldByHosp")
	public String toOrderInfoOldByHosp() {
		return "/trading/pur/purOrderInfoOldByHosp";
	}
	
	/**
	 * @category 获取历史订单数据[医疗机构端]
	 * 应晓川
	 * @date 2015年7月9日
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	@RequestMapping("/getOrdersDetailListOldByHosp")
	@ResponseBody
	public Pagination getOrdersDetailListOldByHosp() {
		log.info("enter getOrdersDetailListOldByHosp method");
		Pagination page = new Pagination(request);
		SysUser user = (SysUser) request.getSession().getAttribute(Constants.USERINFO);
		String hospId = ((HospInfo)user.getInfo()).getHospId();
		Enumeration paramNames = request.getParameterNames();
		try {
			while (paramNames.hasMoreElements()) {
				String paramName = (String) paramNames.nextElement();
				String[] paramValues = request.getParameterValues(paramName);
				if (paramValues.length == 1) {
					String paramValue = paramValues[0].replace("+", "");
					page.getConditions().put(paramName, paramValue);
				}
			}
			page.getConditions().put("hospId", hospId);
			page = purOrderInfoOldManager.getOrdersDetailListOldByHosp(page);
			page.setSuccess(true);
		} catch (Exception e) {
			log.error("",e);
			page.setSuccess(false);
		}
		return page;
	}
	
	
	
}
